/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
package info.decamps.m2settings.model;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

public class ProxyTableModel extends  ExtendableTableModel  {
    private static final long serialVersionUID = 3556670377861360549L;
    
    public static final int COLUMN_ID = 0;
    
    public static final int COLUMN_USERNAME = 1;
    
    public static final int COLUMN_PASSWORD = 2;
    
    public static final int COLUMN_PROTOCOL = 3;
    
    public static final int COLUMN_HOST = 4;
    
    public static final int COLUMN_PORT = 5;
    
    public static final int COLUMN_NONPROXYHOSTS = 6;
    
    private Proxies proxies=null;
    
    // TODO active
    
    private Logger log = Logger.getLogger(ProxyTableModel.class);
    
    public ProxyTableModel(Proxies proxies) {
        log.debug("Create ProxyTableModel");
        if (proxies==null) { //no proxies declared in XML
            proxies=new Proxies();
        }
        this.proxies = proxies;
        log.debug("there are "+proxies.getProxyCount()+" proxies");
        columnTitles = new String[7];
        columnTitles[COLUMN_ID] = "id";
        columnTitles[COLUMN_USERNAME] = "username";
        columnTitles[COLUMN_PASSWORD] = "password";
        columnTitles[COLUMN_PROTOCOL] = "protocol";
        columnTitles[COLUMN_HOST] = "host";
        columnTitles[COLUMN_PORT] = "port";
        columnTitles[COLUMN_NONPROXYHOSTS] = "nonProxyHosts";
    }
    
    public Class<?> getColumnClass(int index) {
        if (index==COLUMN_PORT) return Integer.class;
        return String.class;
    }
    
    public int getRowCount() {
        return proxies.getProxyCount();
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        Proxy row =getRow(rowIndex);
        switch (columnIndex) {
            case COLUMN_ID:
                return row.getId();
            case COLUMN_USERNAME:
                return row.getUsername();
            case COLUMN_PASSWORD:
                return charToStars(row.getPassword());
            case COLUMN_PROTOCOL:
                return row.getProtocol();
            case COLUMN_HOST:
                return row.getHost();
            case COLUMN_PORT:
                return row.getPort();
            case COLUMN_NONPROXYHOSTS:
                return row.getNonProxyHosts();
                
        }
        return null;
    }
    
    public void setValueAt(Object value, int row, int col) {
        Proxy proxy = getRow(row);
        switch (col) {
            case COLUMN_HOST:
                proxy.setHost((String)value);
                break;
            case COLUMN_ID:
                proxy.setId((String)value);
                break;
            case COLUMN_NONPROXYHOSTS:
                proxy.setNonProxyHosts((String)value);
                break;
            case COLUMN_PASSWORD:
                proxy.setPassword((String)value);
                break;
            case COLUMN_PORT:
                Integer val=null;
                if (value.getClass()==String.class)
                    val=Integer.getInteger((String)value);
                else if (value.getClass()==Integer.class)
                    val=(Integer)value;
                if (val!=null) {
                    proxy.setPort(val.intValue());
                }
                break;
            case COLUMN_PROTOCOL:
                proxy.setProtocol((String)value);
                break;
            case COLUMN_USERNAME:
                proxy.setUsername((String)value);
                break;
        }
    }
    
    
    public void addRow() {
        Proxy vProxy=new Proxy();
        addRow(vProxy);
    }
    
    
    public void removeRows(int[] rowIndexes) {
        for (int i = rowIndexes.length - 1; i >= 0; i--) {
            Proxy vProxy = proxies.getProxy(rowIndexes[i]);
            proxies.removeProxy(vProxy);
            fireTableChanged(new TableModelEvent(this, i, i,
                    TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
            fireTableChanged(new TableModelEvent(this));
        }
    }
    
    
    public Proxy getRow(int rowIndex) {
        return proxies.getProxy(rowIndex);
    }
    
    
    public String getElementName() {
        return "proxy";
    }
    
    public void addRow(Proxy proxy) {
        proxies.addProxy(proxy);
        int rows=getRowCount();
        fireTableRowsInserted(rows, rows);
    }

    public Proxies getProxies() {
        return proxies;
    }
    
}
